package resource

import (
	"regexp"
)

const iOS_Install_Plist_Template = `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="3.0">
    <dict>
        <key>items</key>
        <array>
            <dict>
                <key>assets</key>
                <array>
                    <dict>
                        <key>kind</key>
                        <string>software-package</string>
                        <key>url</key>
                        <string>{{AppPackageUrl}}</string>
                    </dict>
                    <dict>
                        <key>kind</key>
                        <string>display-image</string>
                        <key>needs-shine</key>
                        <false/>
                        <key>url</key>
                        <string>{{AppIconImageUrl}}</string>
                    </dict>
                    <dict>
                        <key>kind</key>
                        <string>full-size-image</string>
                        <key>needs-shine</key>
                        <false/>
                        <key>url</key>
                        <string>{{AppIconImageUrl}}</string>
                    </dict>
                </array>
                <key>metadata</key>
                <dict>
                    <key>bundle-identifier</key>
                    <string>{{AppBundleID}}</string>
                    <key>bundle-version</key>
                    <string>{{AppVersion}}</string>
                    <key>kind</key>
                    <string>software</string>
                    <key>title</key>
                    <string>{{AppName}}</string>
                </dict>
            </dict>
        </array>
    </dict>
</plist>
`

func NewPlistStr(AppPackageUrl, AppIconImageUrl, AppBundleID, AppVersion, AppName string) string {
	reg, _ := regexp.Compile(`{{(.*?)}}`)
	plist := reg.ReplaceAllStringFunc(iOS_Install_Plist_Template, func(s string) string {
		switch s {
		case "{{AppPackageUrl}}":
			return AppPackageUrl
		case "{{AppIconImageUrl}}":
			return AppIconImageUrl
		case "{{AppBundleID}}":
			return AppBundleID
		case "{{AppVersion}}":
			return AppVersion
		case "{{AppName}}":
			return AppName
		}
		return s
	})
	return plist
}
